package com.zhs.gccs.adapter;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;
import com.hjq.shape.view.ShapeImageView;
import com.zhs.gccs.BuildConfig;
import com.zhs.gccs.R;
import com.zhs.gccs.model.GccsTaskData;
import com.zhs.gccs.model.GccsTaskFile;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.SharedPrefHelper;

import cn.hutool.core.util.NumberUtil;

/**
 * Created by chengzf on 2024/7/21.
 */
public class GccsDataListAdapter extends BaseQuickAdapter<GccsTaskData, BaseViewHolder> {

    public GccsDataListAdapter() {
        super(R.layout.item_gccs_data_view);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, GccsTaskData bean) {
        holder.setGone(R.id.view_flag, holder.getBindingAdapterPosition() != 0);
        holder.setText(R.id.tv_task_num, holder.getBindingAdapterPosition() + 1 + "");
        holder.setText(R.id.tv_task_time,"采集时间："+ bean.getCreateTime());
        String gccs = "";
        if(!TextUtils.isEmpty(bean.getGccsTypeName())){
            gccs = bean.getGccsTypeName();
        }
        holder.setText(R.id.tv_task_type, "工程措施："+ gccs);
        holder.setText(R.id.tv_task_local, "采集位置："+ NumberUtil.decimalFormat("#.####",
                Double.parseDouble(bean.getLongitude())) +"°E"
                +" "+ NumberUtil.decimalFormat("#.####",Double.parseDouble(bean.getLatitude())) +"°N");
        String qualified = "";
        if ("1".equals(bean.getQualified())){
            qualified = "合格";
        }else if ("0".equals(bean.getQualified())){
            qualified = "不合格";
        }
        holder.setText(R.id.tv_task_result, "抽检结果："+ qualified);
        //
        ShapeImageView iv_image = holder.getView(R.id.iv_image);
        if(bean.getFileList()!=null&&bean.getFileList().size()>0){
            GccsTaskFile image = bean.getFileList().get(0);
            if(TextUtils.isEmpty(image.getLocalUrl())){
                String path = BuildConfig.BASEURL+"gccsFile/preview/image/"+image.getId()
                        +"?token="+ SharedPrefHelper.getInstance().getToken();
                Glide.with(mContext). load(path).into(iv_image);
            }else {
                Glide.with(mContext). load(image.getLocalUrl()).into(iv_image);
            }
        }else {
            Glide.with(mContext). load(R.mipmap.image_2).into(iv_image);
        }
        EasySwipeMenuLayout esml = holder.getView(R.id.esml);
        View content = holder.getView(R.id.content);
        View tv_delete = holder.getView(R.id.tv_delete);
        holder.addOnClickListener(R.id.content);
//        holder.addOnClickListener(R.id.tv_delete);
        content.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(operationListener!=null){
                    operationListener.onItemClick(bean);
                }
            }
        });
        tv_delete.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(operationListener!=null){
                    operationListener.onDelete(bean);
                }
                esml.resetStatus();
            }
        });
    }
    private ItemOperationListener operationListener;

    public void setOperationListener(ItemOperationListener operationListener) {
        this.operationListener = operationListener;
    }

    public interface ItemOperationListener{
        void onDelete(GccsTaskData bean);

        void onItemClick(GccsTaskData bean);
    }
}
